package com.woniu.inlet.timer;

import com.woniu.adapter.ProductBuyAdapter;
import com.woniu.outlet.client.SendOrderMsgToPayFeign;
import com.woniu.outlet.client.mq.OrderPayMQ;
import com.woniu.outlet.mysql.po.ProductBuyMySQL;
import com.woniu.utils.DateUtil;
import com.woniu.utils.IsWorkingDayUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 每天10点发送符合条件的订单信息到可靠消息服务
 */
@Component
@Slf4j
public class SendOrderMsgToPayTimer {

    @Autowired
    private SendOrderMsgToPayFeign sendOrderMsgToPayFeign;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ProductBuyAdapter buyAdapter;


    /**
     * 每天23:30点发送符合条件的订单信息到可靠消息服务
     */
    @Scheduled(cron = "0 30 23 ? * *") //每天23:30点触发
    public void sendMessage() {
        log.debug("发送【符合条件的订单信息到支付模块进行购买】定时任务开始，当前时间{}", DateUtil.getBuyTime());
        //判断当前日期是否是工作日
        String isWorkingDay = IsWorkingDayUtil.getIsWorkingDay(DateUtil.getTodayDate());
        String s = redisTemplate.opsForValue().get("order-day");
        if(StringUtils.isBlank(s)){
            redisTemplate.opsForValue().set("order-day", "0");
        }
        int orderDay = Integer.parseInt(redisTemplate.opsForValue().get("order-day"));
        if (isWorkingDay.equals("y")) {
            log.debug("今天是工作日,间隔天数={}",orderDay);
            String sendDate =DateUtil.getSendDate();
            String[] split = sendDate.split("-");
            //得到 年 月 日
            int year = Integer.parseInt(split[0]);
            int month = DateUtil.getStringFormatToInt(split[1]);
            int day = DateUtil.getStringFormatToInt(split[2]);
            int startDay = day - orderDay - 1;
            int endDay = day;
            String startTime = DateUtil.getCalculateTime(year, month, startDay);
            String endTime = DateUtil.getCalculateTime(year, month, endDay);
            log.debug("要发送订单时间的范围是{}<--->{}",startTime,endTime);
            //查询符合条件的订单信息
            List<ProductBuyMySQL> allTimeCondition = buyAdapter.getAllTimeCondition(startTime, endTime);
            if(allTimeCondition.size()==0){
                log.debug("没有要发送的订单，进入下一轮定时任务");
                return;
            }
            //把需要的数据转为string
            String listToString = getListToString(allTimeCondition);
            log.debug("要发送的订单信息有{}条，为：{}",allTimeCondition.size(),listToString);
            //发送消息
            sendOrderMsgToPayFeign.sendMessage(OrderPayMQ.EXCHANGE,OrderPayMQ.ROUTING_KEY,listToString);
            //redisTemplate.opsForValue().set("listToString",listToString);
            log.debug("发送消息成功");
            //发送消息成功，间隔天数归0
            redisTemplate.opsForValue().set("order-day", "0");
        } else {
            //不是工作日，间隔天数+1
            orderDay = orderDay + 1;
            //redisTemplate.opsForValue().set("order-day", orderDay + "");
            log.debug("今天不是工作日，间隔天数+1,[order-day]+1={}",orderDay);
        }
    }


    public String getListToString(List<ProductBuyMySQL> lists){
        StringBuilder sb=new StringBuilder();
        for(ProductBuyMySQL p : lists){
            String s=p.getId()+"/"+p.getUserId()+"/"+p.getBuyAmount()+"/"+p.getProductType()+"/"+p.getCodeId()+"/"+p.getPayId()+",";
            sb.append(s);
        }
        return sb.toString();
    }



}
